package cn.edu.hust.sse.software7.class3.common;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.google.appengine.api.mail.MailService;
import com.google.appengine.api.mail.MailService.Attachment;
import com.google.appengine.api.mail.MailServiceFactory;

public class EmailTool {
	MailService ms = MailServiceFactory.getMailService();
	private static EmailTool et;
	private Message msg;

	public EmailTool() {
//		et = new EmailTool();
	}

	public static EmailTool getET() {
		if (et == null)
			new EmailTool();
		return et;
	}

	public Message sendEmail(String msgBody, String targetAddr,
			String subject, Attachment attach) {
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);
		Message msg = new MimeMessage(session);
		try {
			msg.setFrom(new InternetAddress("dongxu.m@gmail.com"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
					"dongxu.m@gmail.com"));
			msg.setSubject(subject);
			msg.setText(msgBody);
			Transport.send(msg);
		} catch (MessagingException e) {
			e.printStackTrace();
		}        
		return msg;
	}
	
	public static void test(String... args){
		new EmailTool().sendEmail("Demo", "demo", "test", null);
	}
}
